﻿namespace NEnsure.Builders
{
	public class CustomBrokenRuleBuilder : IBrokenRuleBuilder
	{
		private readonly IErrorProvider _errorProvider;

		public CustomBrokenRuleBuilder(IErrorProvider errorProvider)
		{
			_errorProvider = errorProvider;
		}

		public BrokenRule Build<T>(IBusinessRule<T> rule, string propertyName, T propertyValue)
		{
			string customErrorFormat = _errorProvider.GetError(rule);

			if (!string.IsNullOrEmpty(customErrorFormat))
			{
				rule.ErrorFormat = customErrorFormat;
			}

			return new BrokenRule(propertyName, propertyValue, rule);
		}
	}
}
